package org.dromara.hrp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.hrp.domain.HrpPartner;
import org.dromara.hrp.domain.bo.HrpPartnerBo;
import org.dromara.hrp.domain.vo.HrpPartnerVo;
import org.dromara.hrp.mapper.HrpPartnerMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 合作伙伴Service业务层处理
 *
 * @author wenrui
 * @date 2024-03-15
 */
@RequiredArgsConstructor
@Service
public class HrpPartnerServiceImpl extends BaseService {

  private final HrpPartnerMapper baseMapper;

  /**
   * 查询合作伙伴
   */
  public HrpPartnerVo queryById(Long id) {
    return baseMapper.selectVoById(id);
  }

  /**
   * 查询合作伙伴列表
   */
  public TableDataInfo<HrpPartnerVo> queryPageList(HrpPartnerBo bo, PageQuery pageQuery) {
    LambdaQueryWrapper<HrpPartner> lqw = buildQueryWrapper(bo);
    Page<HrpPartnerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
    return TableDataInfo.build(result);
  }

  /**
   * 查询合作伙伴列表
   */
  public List<HrpPartnerVo> queryList(HrpPartnerBo bo) {
    LambdaQueryWrapper<HrpPartner> lqw = buildQueryWrapper(bo);
    return baseMapper.selectVoList(lqw);
  }

  private LambdaQueryWrapper<HrpPartner> buildQueryWrapper(HrpPartnerBo bo) {
    Map<String, Object> params = bo.getParams();
    LambdaQueryWrapper<HrpPartner> lqw = Wrappers.lambdaQuery();
    lqw.eq(bo.getParentId() != null, HrpPartner::getParentId, bo.getParentId());
    lqw.like(StringUtils.isNotBlank(bo.getName()), HrpPartner::getName, bo.getName());
    lqw.eq(StringUtils.isNotBlank(bo.getMobile()), HrpPartner::getMobile, bo.getMobile());
    lqw.eq(bo.getIsSupplier() != null, HrpPartner::getIsSupplier, bo.getIsSupplier());
    lqw.eq(bo.getIsCustomer() != null, HrpPartner::getIsCustomer, bo.getIsCustomer());
    lqw.eq(bo.getIsManufacturer() != null, HrpPartner::getIsManufacturer, bo.getIsManufacturer());
    lqw.eq(StringUtils.isNotBlank(bo.getStatus()), HrpPartner::getStatus, bo.getStatus());
    lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
      HrpPartner::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
    return lqw;
  }

  /**
   * 新增合作伙伴
   */
  public Boolean insertByBo(HrpPartnerBo bo) {
    HrpPartner add = MapstructUtils.convert(bo, HrpPartner.class);
    validEntityBeforeSave(add);
    boolean flag = baseMapper.insert(add) > 0;
    if (flag) {
      bo.setId(add.getId());
    }
    return flag;
  }

  /**
   * 修改合作伙伴
   */
  public Boolean updateByBo(HrpPartnerBo bo) {
    HrpPartner update = MapstructUtils.convert(bo, HrpPartner.class);
    validEntityBeforeSave(update);
    return baseMapper.updateById(update) > 0;
  }

  /**
   * 保存前的数据校验
   */
  private void validEntityBeforeSave(HrpPartner entity) {
    //TODO 做一些数据校验,如唯一约束
  }

  /**
   * 批量删除合作伙伴
   */
  public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    return baseMapper.deleteBatchIds(ids) > 0;
  }
}
